using System.Collections.Generic;
using System.Text;

namespace EF6Demo.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using EF6Demo.Model;

    internal sealed class Configuration : DbMigrationsConfiguration<EF6Demo.BlogContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(EF6Demo.BlogContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //

            var _tmp = new List<Blog>
            {
                new Blog {BlogId = 1, Title = "t123456789", Url = "www.baidu.com", Author = "Liyy",
                    Category = BlogCategory.CSharp,
                    BlogDetail=new BlogDetails
                    {
                        DateCreated=DateTime.Now,
                        Description="Desc1",
                        TimeStamp=Encoding.GetEncoding("gbk").GetBytes("ts1")
                    },
                    Posts=new List<Post>{
                        new Post{ Content="c11",
                            CreatedBy=new Person{Name="Li"},
                            DateCreated=DateTime.Now,
                            Rating=4,
                            Title="T111"
                        },
                        new Post{ Content="c12",
                            CreatedBy=new Person{Name="Li"},
                            DateCreated=DateTime.Now,
                            Rating=4,
                            Title="T112"
                        }
                    }
                },
                new Blog {BlogId = 2, Title = "t223456789", Url = "c2", Author = "Liyy", 
                    Category = BlogCategory.JAVA,
                    BlogDetail=new BlogDetails
                    {
                        DateCreated=DateTime.Now,
                        Description="Desc2",
                        TimeStamp=Encoding.GetEncoding("gbk").GetBytes("ts2")
                    },
                    Posts=new List<Post>{
                    new Post{ Content="c21",
                        CreatedBy=new Person{Name="Li"},
                        DateCreated=DateTime.Now,
                        Rating=4,
                        Title="T211"
                    },
                    new Post{ Content="c22",
                        CreatedBy=new Person{Name="Li"},
                        DateCreated=DateTime.Now,
                        Rating=4,
                        Title="T212"
                    }
                }},
                new Blog {BlogId = 3, Title = "t323456789", Url = "c3", Author = "Liyy", 
                    Category = BlogCategory.Live,
                    BlogDetail=new BlogDetails
                    {
                        DateCreated=DateTime.Now,
                        Description="Desc3",
                        TimeStamp=Encoding.GetEncoding("gbk").GetBytes("ts3")
                    },
                    Posts=new List<Post>{
                    new Post{ Content="c31",
                        CreatedBy=new Person{Name="Li"},
                        DateCreated=DateTime.Now,
                        Rating=4,
                        Title="T311"
                    },
                    new Post{ Content="c32",
                        CreatedBy=new Person{Name="Li"},
                        DateCreated=DateTime.Now,
                        Rating=4,
                        Title="T312"
                    }
                }},
                new Blog {BlogId = 6, Title = "t423456789", Url = "c4", Author = "Liyy",
                    Category = BlogCategory.MSSQL,
                    BlogDetail=new BlogDetails
                    {
                        DateCreated=DateTime.Now,
                        Description="Desc4",
                        TimeStamp=Encoding.GetEncoding("gbk").GetBytes("ts4")
                    },
                    Posts=new List<Post>{
                    new Post{ Content="c41",
                        CreatedBy=new Person{Name="Li"},
                        DateCreated=DateTime.Now,
                        Rating=4,
                        Title="T411"
                    },
                    new Post{ Content="c42",
                        CreatedBy=new Person{Name="Li"},
                        DateCreated=DateTime.Now,
                        Rating=4,
                        Title="T412"
                    }
                }},
            };

            _tmp.ForEach(s => context.Blogs.AddOrUpdate(p => p.Title, s));
            context.SaveChanges();
        }
    }
}
